package com.cloudspace.yunloan.utils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cloudspace.yunloan.YunloanApp;
import com.cloudspace.yunloan.R;


public class MyDialog extends Dialog {
    private Context context;
    TextView mMessageView;

    public MyDialog(Context context, int theme) {
        super(context, theme);
        this.context = context;
    }

    public MyDialog(Context context) {
        super(context);
        this.context = context;
    }

    /**
     * Helper class for creating a custom dialog
     */
    public static class Builder {
        private Context context;
        private String title; // 对话框标题
        private String message; // 对话框内容
        private String backButtonText; // 对话框返回按钮文本
        private String confirmButtonText; // 对话框确定文本
        private View contentView;
        private boolean cancel = true;

        // 对话框按钮监听事件
        private OnClickListener backButtonClickListener,
                confirmButtonClickListener;

        public Builder(Context context) {
            this.context = context;
        }

        /**
         * 对话框是否可以取消
         *
         * @param cancel
         */
        public void setCancelable(boolean cancel) {
            this.cancel = cancel;
        }


        /**
         * 使用字符串设置对话框消息
         *
         * @param message
         * @return
         */
        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }

        /**
         * 使用资源设置对话框消息
         *
         * @param message
         * @return
         */
        public Builder setMessage(int message) {
            this.message = (String) context.getText(message);
            return this;
        }

        /**
         * 使用资源设置对话框标题信息
         *
         * @param title
         * @return
         */
        public Builder setTitle(int title) {
            this.title = (String) context.getText(title);
            return this;
        }

        /**
         * 使用字符串设置对话框标题信息
         *
         * @param title
         * @return
         */
        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        /**
         * 设置自定义的对话框内容
         *
         * @param v
         * @return
         */
        public Builder setContentView(View v) {
            this.contentView = v;
            return this;
        }

        /**
         * 设置back按钮的事件和文本
         *
         * @param backButtonText
         * @param listener
         * @return
         */
        public Builder setLeftButton(String backButtonText,
                                     OnClickListener listener) {
            this.backButtonText = backButtonText;
            this.backButtonClickListener = listener;
            return this;
        }

        /**
         * 设置确定按钮事件和文本
         *
         * @param
         * @param listener
         * @return
         */
        public Builder setRightButton(String confirmButtonText,
                                      OnClickListener listener) {
            this.confirmButtonText = confirmButtonText;
            this.confirmButtonClickListener = listener;
            return this;
        }

        public MyDialog create() {
            int width = YunloanApp.getInstance().getScreenWidth() * 4 / 10;
            return this.create(width, View.VISIBLE);
        }

        /**
         * 创建自定义的对话框
         */
        @SuppressLint("InflateParams")
        public MyDialog create(int width, int processLayoutVisibilty) {
            if (context == null) return null;
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // 实例化自定义的对话框主题
            final MyDialog dialog = new MyDialog(context, R.style.mydialog_style);
            View layout = inflater.inflate(R.layout.dialog_scan, null);
            dialog.addContentView(layout, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

            WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
            lp.width = width; //设置宽度
            dialog.getWindow().setAttributes(lp);
            TextView titleView = ((TextView) layout.findViewById(R.id.dialog_title));
            TextView content = (TextView) layout.findViewById(R.id.dialog_content_text);

            // 设置对话框标题
            if (StringUtil.isEmpty(title)) {
                titleView.setVisibility(View.GONE);
                content.setPadding(content.getPaddingLeft(), content.getPaddingTop() + 20, content.getPaddingRight(), content.getPaddingBottom());
            } else {
                titleView.setText(Html.fromHtml(title), TextView.BufferType.SPANNABLE);
            }
            // 设置对话框内容
            if (!StringUtil.isEmpty(message)) {
                content.setText(message);
            } else if (contentView != null) {
                // if no message set
                // 如果没有设置对话框内容，添加contentView到对话框主体
                ((RelativeLayout) layout.findViewById(R.id.dialog_content))
                        .removeAllViews();
                ((RelativeLayout) layout.findViewById(R.id.dialog_content))
                        .addView(contentView, new LayoutParams(
                                LayoutParams.MATCH_PARENT,
                                LayoutParams.WRAP_CONTENT));
            }

            layout.findViewById(R.id.divider_line).setVisibility(processLayoutVisibilty);
            layout.findViewById(R.id.process_layout).setVisibility(processLayoutVisibilty);
            // 设置返回按钮事件和文本
            int color = context.getResources().getColor(R.color.color_1e9ef7);
            if (backButtonText != null) {
                TextView bckButton = ((TextView) layout
                        .findViewById(R.id.cancel_btn));
                bckButton.setText(backButtonText);
                if (backButtonClickListener != null) {
                    bckButton.setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {
                            backButtonClickListener.onClick(dialog,
                                    DialogInterface.BUTTON_NEGATIVE);
                            dialog.dismiss();
                        }
                    });
                }
            } else {
                layout.findViewById(R.id.cancel_btn).setVisibility(View.GONE);
                layout.findViewById(R.id.btn_devide).setVisibility(View.GONE);
                layout.findViewById(R.id.confirm_btn).setBackgroundResource(
                        R.drawable.confirm_single_select);
            }

            // 设置确定按钮事件和文本
            if (confirmButtonText != null) {
                TextView cfmButton = ((TextView) layout
                        .findViewById(R.id.confirm_btn));
                cfmButton.setText(confirmButtonText);
                cfmButton.setTextColor(color);
                if (confirmButtonClickListener != null) {
                    cfmButton.setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {
                            confirmButtonClickListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
                            if (dialog != null && dialog.isShowing()) {
                                dialog.dismiss();
                            }
                        }
                    });
                }
            } else {
                layout.findViewById(R.id.confirm_btn).setVisibility(View.GONE);
            }
            dialog.setContentView(layout);

            if (!cancel) {
                dialog.setCancelable(false);
                dialog.setCanceledOnTouchOutside(false);
            }

            return dialog;
        }
    }

    public MyDialog setMessage(CharSequence message) {
        if (mMessageView == null) {
            mMessageView = (TextView) getWindow().getDecorView().findViewById(R.id.dialog_content_text);
        }
        if (mMessageView != null) {
            mMessageView.setText(message);
        }
        return this;
    }

    @Override
    public void show() {
        try {
            if (!isInvaid(context)) {
                super.show();
            }
        } catch (RuntimeException e) {
        }
    }

    public static boolean isInvaid(Context context) {
        return context == null || ((context instanceof Activity) && ((Activity) context).isFinishing());
    }

    @Override
    public void dismiss() {
        super.dismiss();

        MMAlert.setIsShowing(false);
    }
}